#include "SettingsWindow.h"
#include "GeneralTab.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QMessageBox>

///////////////////////////////////////////////////////////////////////

SettingsWindow::SettingsWindow(QList<NetworkPlugin*> *_networkPluginList, QWidget *parent):
QDialog(parent), networkPluginList(_networkPluginList)
{
    networkPluginTabList = new QList<NetworkPluginTab*>;
    tabWidget = new QTabWidget;
    tabWidget->addTab(new GeneralTab(), tr("Ogolne"));

    NetworkPluginTab *tmpTab;

    for(QList<NetworkPlugin*>::iterator i = networkPluginList->begin(); i!=networkPluginList->end(); i++)
    {
        tabWidget->addTab(  tmpTab = new NetworkPluginTab(*i,this),
                            (*i)->getPluginName()                 );

        networkPluginTabList->push_back(tmpTab);

        connect(tmpTab->loginLineEdit,SIGNAL(textChanged(QString)),this ,SLOT(modification()));
        connect(tmpTab->passLineEdit,SIGNAL(textChanged(QString)),this ,SLOT(modification()));
    }

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(tabWidget);


        QHBoxLayout *buttonsLayout = new QHBoxLayout;
            buttonsLayout->addWidget(okButton = new QPushButton("Ok",this));
            buttonsLayout->addWidget(cancelButton = new QPushButton("Anuluj",this));
            buttonsLayout->addWidget(applyButton = new QPushButton("Zastosuj",this));
            applyButton->setEnabled(false);

            connect(okButton, SIGNAL(clicked()),this,SLOT(okFunction()));
            connect(cancelButton, SIGNAL(clicked()),this,SLOT(cancelFunction()));
            connect(applyButton, SIGNAL(clicked()),this,SLOT(applyFunction()));


    mainLayout->addLayout(buttonsLayout);


    setLayout(mainLayout);

}
///////////////////////////////////////////////////////////////////////
void SettingsWindow::okFunction()
{
    applyFunction();
    cancelFunction();
}
///////////////////////////////////////////////////////////////////////
void SettingsWindow::cancelFunction()
{
    for(QList<NetworkPluginTab*>::iterator i = networkPluginTabList->begin(); i!=networkPluginTabList->end(); i++)
    {
        (*i)->cancelFunction();
    }

    this->hide();

    tabWidget->setCurrentIndex (0) ;
    applyButton->setEnabled(false);
}
///////////////////////////////////////////////////////////////////////
void SettingsWindow::applyFunction()
{
    for(QList<NetworkPluginTab*>::iterator i = networkPluginTabList->begin(); i!=networkPluginTabList->end(); i++)
    {
        (*i)->applyFunction();
    }

    applyButton->setEnabled(false);
}
//////////////////////////////////////////////////////////////////////
void SettingsWindow::modification()
{
    applyButton->setEnabled(true);
}
